package cn.credit.loan.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.credit.loan.entity.Manager;
import cn.credit.loan.service.manager.ManagerService;

/**
 * 业务经理控制类
 * @author 
 *
 */
@Controller
public class ManagerController {
	//日志输出
	private Logger logger = Logger.getLogger(ManagerController.class);
	@Autowired
	private ManagerService managerService;
	/**
	 * 业务经理登录
	 * @param managerName
	 * @param managerPwd
	 * @param model
	 * @return
	 */
	@RequestMapping("/mangerdologin")
	public ModelAndView login(@RequestParam("adminName")String managerName,
					    @RequestParam("adminPwd")String managerPwd,Model model,HttpSession session){
			try {
				logger.info("####-------- >> 处理业务经理的");
				Manager manager=managerService.managerlogin(managerName, managerPwd);
				if(manager!=null){
					if(!manager.getManagerPwd().equals(managerPwd)){
						model.addAttribute("errorpwd", "密码输入不正确");
						return new ModelAndView("after/login","roleModel",model);
					}
					session.setAttribute("manager", manager);
					session.setAttribute("role",3);
					session.setAttribute("rolelogin", "业务经理");
					return new ModelAndView("after/index","roleModel",session);
				}else{
					model.addAttribute("errorName", "业务经理不存在");
				}
			} catch (Exception e) {
				model.addAttribute("syserror","出现错误，请联系管理员");
				logger.info("####------->> error =" +"出现错误，请联系管理员");
				e.printStackTrace();
			}
			return new ModelAndView("after/login","roleModel",model);
			//return "after/login";
	}
	/**
	 * 业务经理添加
	 * @param manager
	 * @param model
	 * @return
	 */
	@RequestMapping("/addmanager")
	public String addManager(Manager manager,ModelMap model)
	{
		try {
			
			boolean managers=managerService.manageradd(manager);
			if(managers){
			model.addAttribute("manager", manager);
			return "Manageradd";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		}
	/**
	 * 业务经理更新
	 * @param manager
	 * @param model
	 * @return
	 */
	@RequestMapping("/updatemanager")
	public String updateManager(Manager manager,ModelMap model)
	{
		try {
			
			boolean managerupdate=managerService.managerupdate(manager);
			if(managerupdate){
			model.addAttribute("manager", manager);
			return "Managerupdate";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 业务经理删除
	 * @param managerId
	 * @param model
	 * @return
	 */
	@RequestMapping("/deletemanager")
	public String deleteManager(int managerId,ModelMap model)
	{
		try {
			
			boolean managerdelete=managerService.managerdelete(managerId);
			if(managerdelete){
			model.addAttribute("managerId", managerId);
			return "Managerdelete";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
}
	
